<template>
	<div class="diy_edit logistics_delivery_edits" id="logistics_delivery_edit">
	  <el-form
		:model="form"
		ref="ruleFormRef"
		:rules="rules"
		label-width="auto"
		class="edit_logistics_delivery_form"
		size="large"
    v-if="is_view()"
	  >
	  <el-row class="row_ce">
        <el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
          <el-form-item label="订单号" prop="order_number">
            <el-input id="order_number" v-model="form['order_number']" placeholder="请输入订单号"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
          <el-form-item label="商品名称" prop="product_name">
            <el-input id="product_name" v-model="form['product_name']" placeholder="请输入商品名称"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
          <el-form-item label="购买数量" prop="purchase_quantity">
            <el-input id="purchase_quantity" v-model="form['purchase_quantity']" placeholder="请输入购买数量"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
          <el-form-item label="交易总额" prop="total_transaction_amount">
            <el-input-number id="total_transaction_amount" v-model.number="form['total_transaction_amount']"
            ></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
          <el-form-item label="发货日期" prop="the_date_of_issuance">
            <el-date-picker id="the_date_of_issuance"
                            v-model="form['the_date_of_issuance']" type="date" placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
          <el-form-item label="配送单号" prop="delivery_number">
            <el-input id="delivery_number" v-model="form['delivery_number']" placeholder="请输入配送单号"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
          <el-form-item label="联系人" prop="contact_name">
            <el-input id="contact_name" v-model="form['contact_name']" placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
          <el-form-item label="买家" prop="ordinary_users">
            <el-select v-model="form.ordinary_users" placeholder="请选择" :disabled="true">
              <el-option :key="-1" label="未选择" :value="0">
              </el-option>
              <el-option v-for="(o,i) in list_user" :key="i" :label="o.nickname" :value="o.user_id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
          <el-form-item label="收货地址" prop="shipping_address">
            <el-input id="shipping_address" v-model="form['shipping_address']" placeholder="请输入收货地址"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
          <el-form-item label="配送状态" prop="delivery_status">
            <el-select id="delivery_status" v-model="form['delivery_status']"
            >
              <el-option v-for="o in list_delivery_status" :key="o" :label="o" :value="o">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
          <el-form-item label="签收状态" prop="signing_status">
            <el-select id="signing_status" v-model="form['signing_status']"
                       :disabled="true"
            >
              <el-option v-for="o in list_signing_status" :key="o" :label="o" :value="o">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>


      </el-row>
		<div class="footer">
		  <el-button type="primary" @click="Onsubmit" size="large" v-if="$check_action('/logistics_delivery/view','set') || $check_action('/logistics_delivery/view','add')"
			>提交</el-button
		  >
      <el-button @click="cancel()">返回</el-button>
		</div>
	  </el-form>
	</div>
  </template>
  
  <script setup>
  import { ElMessage } from "element-plus";
  import { ref, onMounted } from "vue";
  import { $post, $get } from "@/api/Api.js";
  import storageHelper from "@/libs/StorageHelper";
  import { useStore } from "vuex";
  import { useRouter } from "vue-router";
  import {
  $toTime,
	$fullUrl,
	formatDate,
	$banseUrl,
	$check_action,
	$getUrlParams,
  } from "@/utils/utils.js";
  let router = useRouter();
  let store = useStore();
  let token = storageHelper.getToken() || null;
  let UserInfo = storageHelper.getUserInfo();
  let user_group = storageHelper.getGroupValue();
  let logistics_delivery_id = $getUrlParams().logistics_delivery_id;
  let url_add = ref("/api/logistics_delivery/add");
  let url_set = ref("/api/logistics_delivery/set");
  let url_get_obj = ref("/api/logistics_delivery/get_obj");
  let url_upload = ref("/api/logistics_delivery/upload");
  let title = ref("logistics_delivery");
  let field = ref("logistics_delivery_id");
  
  let form = ref({
    "order_number": '', // 订单号
    "product_name": '', // 商品名称
    "purchase_quantity": '', // 购买数量
    "total_transaction_amount": 0, // 交易总额
    "the_date_of_issuance": '', // 发货日期
    "delivery_number": '', // 配送单号
    "ordinary_users": 0, // 普通用户
    "shipping_address": '', // 收货地址
    "delivery_status": '', // 配送状态
    "signing_status": '', // 签收状态
    "logistics_delivery_id": 0, // ID
    "contact_name": "", // ID
  });
  let disabledObj=ref({
    "order_number_isDisabled": false,
    "product_name_isDisabled": false,
    "purchase_quantity_isDisabled": false,
    "total_transaction_amount_isDisabled": false,
    "the_date_of_issuance_isDisabled": false,
    "delivery_number_isDisabled": false,
    "ordinary_users_isDisabled": false,
    "shipping_address_isDisabled": false,
    "delivery_status_isDisabled": false,
    "signing_status_isDisabled": false,


  })
  // 配送状态列表
let list_delivery_status = ref(['待取货', '配送中', '已完成']);
// 签收状态列表
let list_signing_status = ref(['待签收', '已签收']);
let list_user = ref([]);
let ruleFormRef = ref(null);
  let rules = ref({
	name: [
	  {
		required: true,
		message: "链接名不能为空",
		trigger: "blur",
	  },
	],
	url: [
	  {
		required: true,
		message: "连接不能为空",
		trigger: "blur",
	  },
	],
  });
  //获取对象之前
  let get_obj_before=(param)=>{
      if (form.value["the_date_of_issuance"].indexOf("-") === -1) {
        form.value["the_date_of_issuance"] = $toTime(parseInt(form.value["the_date_of_issuance"]), "YYYY-MM-DD")
      }
      return param;
    }
  //获取对象之后
  let  get_obj_after=()=>{
    if (form.value["the_date_of_issuance"] == "0000-00-00") {
      form.value["the_date_of_issuance"] = null;
    }
    if (parseInt(form.value["the_date_of_issuance"]) > 9999) {
      form.value["the_date_of_issuance"] = $toTime(parseInt(form.value["the_date_of_issuance"]), "YYYY-MM-DD")
    }

}
  //提交前验证事件 
  // let submit_check=(param)=>{
  //     let msg = null
  //     return msg;
  // }
  
  let is_view=() =>{
      var bl = user_group == "管理员";

      if (!bl) {
        bl = $check_action('/logistics_delivery/table', 'add');
        console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
      }
      if (!bl) {
        bl = $check_action('/logistics_delivery/table', 'set');
        console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
      }
      if (!bl) {
        bl = $check_action('/logistics_delivery/view', 'add');
        console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
      }
      if (!bl) {
        bl = $check_action('/logistics_delivery/view', 'set');
        console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
      }
      if (!bl) {
        bl = $check_action('/logistics_delivery/view', 'get');
        console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
      }

      console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

      return bl;
    }
  // 点击取消
  let cancel = () => {
	router.go(-1);
  };
  // 点击提交
  let Onsubmit = async () => {
    // submit_check()
	let valid = await ruleFormRef.value.validate();
	if (valid) {
	  if(logistics_delivery_id){
		  // 编辑
      delete form.value.update_time;
		  $post(url_set.value+"?logistics_delivery_id="+logistics_delivery_id, form.value).then((res) => {
			  if (res.result) {
			  ruleFormRef.value.resetFields();
			  router.push("./table");
			  ElMessage.success("提交成功！");
			  } else if (res.error) {
			  ElMessage.error(res.error.message);
			  }
		  });
	   
	  }else{
      // 新增
      $post(url_add.value, form.value).then((res) => {
			  if (res.result) {
			  ruleFormRef.value.resetFields();
			  router.push("./table");
			  ElMessage.success("提交成功！");
			  } else if (res.error) {
			  ElMessage.error(res.error.message);
			  }
		  });

    }
	} else {
	  ElMessage.error(msg);
	}
  };
  // 获取详情
  let get_logistics_delivery_obj = async () => {
  get_obj_before()
	var list = await $get("/api/logistics_delivery/get_obj?logistics_delivery_id=" + logistics_delivery_id);
  form.value = list.result.obj;
  get_obj_after()

};
  //获取用户
let get_list_user = async() => {
  var json =  await $get('/api/user/get_list')
  if (json.result) {
        list_user.value = json.result.list;
      } else if (json.error) {
        console.error(json.error);
      }

};
  onMounted(() => {
    get_list_user()
    if(logistics_delivery_id){
      get_logistics_delivery_obj();
    }
  });
  </script>
  
  <style scoped lang="less">
  .diy_edit {
	width: 100%;
	.edit_logistics_delivery_form {
	  max-width: 60%;
	  margin: 90px auto;
	  .fn {
		width: 100%;
	  }
	}
	img {
	  width: 100px;
	  height: 100px;
	}
	.edit_list_logistics_delivery_sell {
	  width: 80%;
	  margin: 90px auto;
	}
	.footer {
	  text-align: center;
	}
  }
  </style>
  